<template>
  <div class="reco-page">
    <el-button class="back-btn" text circle @click="goBack">
      <el-icon><ArrowLeft /></el-icon>
    </el-button>

    <div class="page-header">
      <h2>AI推荐</h2>
      <div class="actions">
        <el-input v-model="query.paramKey" placeholder="参数Key" style="width:200px;margin-right:8px" clearable />
        <el-select v-model="query.recommendationType" placeholder="类型" style="width:160px;margin-right:8px">
          <el-option label="优化建议" value="OPTIMIZATION" />
          <el-option label="趋势预测" value="PREDICTION" />
          <el-option label="异常告警" value="ALERT" />
        </el-select>
        <el-button type="primary" :loading="loading" @click="loadData">查询</el-button>
      </div>
    </div>

    <el-table :data="records" v-loading="loading" border style="width: 100%">
      <el-table-column prop="paramId" label="参数ID" width="120" />
      <el-table-column prop="recommendedValue" label="推荐值" min-width="160" />
      <el-table-column prop="currentValue" label="当前值" min-width="160" />
      <el-table-column prop="confidenceScore" label="置信度" width="120" />
      <el-table-column prop="algorithmType" label="类型" width="140" />
      <el-table-column prop="createTime" label="时间" width="180" />
    </el-table>

    <div class="pager">
      <el-pagination
        background
        layout="prev, pager, next, jumper, ->, total"
        :current-page="query.pageNum"
        :page-size="query.pageSize"
        :total="total"
        @current-change="onPageChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getRecommendations } from '@/assets/api/mobile'
import { ArrowLeft } from '@element-plus/icons-vue'

const router = useRouter()
const loading = ref(false)
const records = ref([])
const total = ref(0)
const query = reactive({
  paramKey: '',
  recommendationType: 'OPTIMIZATION',
  pageNum: 1,
  pageSize: 10
})

const normalizePage = (res) => {
  const data = res?.data?.data || res?.data || {}
  return {
    records: Array.isArray(data?.records) ? data.records : (Array.isArray(data?.list) ? data.list : []),
    total: Number(data?.total || 0),
    current: Number(data?.current || data?.pageNum || 1),
    size: Number(data?.size || data?.pageSize || 10)
  }
}

const loadData = async () => {
  loading.value = true
  try {
    const res = await getRecommendations({
      paramKey: query.paramKey || undefined,
      recommendationType: query.recommendationType,
      pageNum: query.pageNum,
      pageSize: query.pageSize
    })
    const page = normalizePage(res)
    records.value = page.records
    total.value = page.total
  } finally {
    loading.value = false
  }
}

const onPageChange = (p) => { query.pageNum = p; loadData() }
const goBack = () => router.push('/param-engine/dashboard')

onMounted(loadData)
</script>

<style scoped>
.reco-page { padding: 12px; position: relative; }
.page-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; padding-left: 44px; position: relative; }
.pager { margin-top: 12px; text-align: right; }
.back-btn { position: absolute; top: 8px; left: 8px; z-index: 10; }
</style>



